package com.yunao.web.thread;

import com.yunao.web.utils.WechatUtils;
import com.yunao.web.wechat.object.AccessToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/*
* 定时刷新access_token (1.5h一次)
* */
@Component
@EnableScheduling
public class AccessTokenThread {
    private static Logger logger = LoggerFactory.getLogger(AccessTokenThread.class);
    @Value("${wechat.appid}")
    private String appid;
    @Value("${wechat.appsecret}")
    private String appsecret;

    @Scheduled(initialDelay = 5_000,fixedRate = 90*60_000)
    public void run(){
        AccessToken accessToken = null;
        int i = 0;//最多请求三次
        while(i<=2){
            accessToken = WechatUtils.getAccessToken(appid, appsecret);
            if (accessToken!=null){
                break;
            }else {
                i++;
                accessToken = WechatUtils.getAccessToken(appid, appsecret);
            }
        }
        WechatUtils.accessToken=accessToken;
    }

}
